package com.xmy.aboutThread;

// 信号量机制

public class Demo5 {

    private static volatile int signal = 0;

    static class ThreadA implements Runnable {

        @Override
        public void run() {
            while (signal < 10) {

                if (signal % 2 == 0) {

                    System.out.println("ThreadA " + signal);

                    signal++;

                }
            }
        }
    }

    static class ThreadB implements Runnable {
        @Override
        public void run() {
            while (signal < 10) {

                if (signal % 2 == 1) {

                    System.out.println("ThreadB " + signal);

                    signal++;

                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException{
        new Thread(new ThreadA()).start();

        Thread.sleep(20);

        new Thread(new ThreadB()).start();
    }
}
